<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.sql.Connection"%>
<%@page import="com.yuxin.common.util.DBUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="com.yuxin.common.util.CommonUtil"%>
<%
	Connection conn = null;
	List<Map<String, Object>> orderList = null;
	TransactionManager tranM = null;
	String redirect = null;
	String action = request.getParameter("action");
	String id = request.getParameter("id");
	try {
		if(!CommonUtil.isLoginValid(request)){
			response.sendRedirect(request.getContextPath()+"/site/denglu.jsp");
			return;
		 }
		
		conn = DBUtil.getConnection();
		 // 页面标题
        request.setAttribute("titleName",URLDecoder.decode("我的订单", "UTF-8"));
		 
		String credit_user_id = CommonUtil.getCookie(request, "TICKET");
		String sql = "select t.ID, t.CODE, t.TOTAL_PRICE, t.STATUS, DATE_FORMAT(t.CREATE_TIME, '%Y-%m-%d %H:%i') as CREATETIME," 
		   			  + " DATE_FORMAT(t.PAY_TIME, '%Y-%m-%d %H:%i') as PAYTIME, DATE_FORMAT(t.SHIPPING_TIME, '%Y-%m-%d %H:%i') as SHIPPINGTIME,"
					  + " DATE_FORMAT(t.TRANSACTION_TIME, '%Y-%m-%d %H:%i') as TRANSACTIONTIME"	
					 + " from p_orders t where t.CREDIT_ID = ? order by t.CREATE_TIME desc";
		orderList = DBUtil.queryList(conn, sql, credit_user_id);
		
		if(id != null && !"".equals(id) && action != null && !"".equals(action)) {
			tranM = new TransactionManager(conn);
			tranM.begin();
			// Close order
			if("close".equals(action)) {
				sql = "update p_orders set STATUS = 6 where id = ?";
				int res = DBUtil.executeSql(conn, sql, id);
				if(res > 0) {
					redirect = request.getContextPath()+"/site/my_order.jsp?res_code=101";
				}
			} else if("receipt".equals(action)) { // Receipt
				sql = "update p_orders set STATUS = 5, TRANSACTION_TIME = ? where id = ?";
				int res = DBUtil.executeSql(conn, sql, new Object[]{new Date(), id});
				if(res > 0) {
					redirect = request.getContextPath()+"/site/my_order.jsp?res_code=102";
				}
			}
		}
	} catch (Exception ex) {
		if(tranM != null) {
			tranM.rollback();
		}
		ex.printStackTrace();
	} finally {
		if(tranM != null) {
			tranM.commit();
		}
		DBUtil.close(conn);
		if(redirect != null) {
			response.sendRedirect(redirect);
		}
	}
%>
<!DOCTYPE html>

<%@page import="com.yuxin.core.TransactionManager"%>
<%@page import="java.util.Date"%><html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>${titleName}</title>
        <meta name="Keywords" content="中信信用卡平台" />
        <meta name="Description" content="${titleName}"/>
        <meta name="robots" content="all"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes"/>
        <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
        <meta name="format-detection" content="telephone=no"/>
        <meta http-equiv="cleartype" content="on"/>
        <link rel="icon" type="image/png" href="images/favicon.ico">
        <%@ include file="/site/css.jsp" %>
        <style type="text/css">
			.biaoge{margin-top:10px;width:100%;background-color:#fff;overflow:hidden;border:1px solid #C6C4C2;line-height:24px;-webkit-border-radius: .2em;-moz-border-radius: .2em;border-radius: .2em;}
			.biaoge li{list-style-type: none;color:#666;padding:1px 10px;border-bottom:1px solid #C6C4C2;line-height:30px;overflow:hidden;}
			.biaoge li:last-child{border-bottom: none;}
			.biaoge .name{float:left;}
			.biaoge .value{float:right;}
		</style>
	</head>
	<body>
        <div id="page">
            <%@ include file="/site/header_nav.jsp" %>
            <div id="container">
              <%
			  	if(orderList == null || orderList.size() == 0) {
			  		out.println("<div style='margin-top:10px; text-align:center;'>暂无订单记录！</div>");
			  	} else {
              		for(Map<String, Object> map : orderList) {
              			String o_status = map.get("STATUS").toString();
			  %> 
              <a href="order_detail.jsp?id=<%=map.get("ID") %>" style="display:block">
                <ul class="biaoge">
                    <li><div class="name">订单编号</div><div class="value"><%=map.get("CODE") %></div></li>
                    <li><div class="name">订单状态</div><div class="value"><%="1".equals(o_status) ? "未付款" : "2".equals(o_status) ? "等待卖家发货" : "3".equals(o_status) ? "卖家已发货" : 
                		"4".equals(o_status) ? "退款中" : "5".equals(o_status) ? "交易成功" : "交易关闭" %></div></li>
                    <li>
                        <div class="name">
                            <%="1".equals(o_status) ? "下单时间" : "2".equals(o_status) ? "付款时间" : "3".equals(o_status) ? "发货时间" : 
                            	"5".equals(o_status) ? "收货时间" : "成交时间" %>
                        </div>
                        <div class="value">
                            <%="1".equals(o_status) ? map.get("CREATETIME") : "2".equals(o_status) ? map.get("PAYTIME") :  
                            	"3".equals(o_status) ? map.get("SHIPPINGTIME") : 
                            	"5".equals(o_status) ? map.get("TRANSACTIONTIME") : map.get("CREATETIME")%>
                         </div>
                    </li>
                    <li><div class="name">订单金额</div><div class="value"><%=map.get("TOTAL_PRICE") %>元</div></li>
                    <%
						if("1".equals(o_status) || "3".equals(o_status)) {
					 %>
                    <li style="border-top:1px solid #C6C4C2;">
                    	<div style="text-align:center; padding-bottom:10px; margin-top:10px;">
                        	<%
								if("1".equals(o_status)) {
							 %>
                             <a class="button big blue" href="pay.jsp?id=<%=map.get("ID") %>">去付款</a>
                              <div style="height:10px;"></div>
                             <a class="button big orange" href="my_order.jsp?id=<%=map.get("ID") %>&amp;action=close">关闭交易</a>
                             <%
								}
                             %>
                             <%
								if("3".equals(o_status)) {
							 %>
                             <a class="button big blue" href="my_order.jsp?id=<%=map.get("ID") %>&amp;action=receipt">确认收货</a>
                              <div style="height:10px;"></div>
                             <a class="button big orange" href="my_order.jsp?id=<%=map.get("ID") %>&amp;action=close">关闭交易</a>
                             <%
								}
                             %>
                        </div>
                    </li>
                    <%
						}
					 %>
                </ul>
              </a>
            <%
              	}
			  }	
            %>
            </div>
            <%@ include file="/site/footer.jsp" %>
        </div>
    </body>
     <%@ include file="/site/js.jsp" %>
     <script language="javascript" type="text/javascript">
	 <!--
	 	$(document).ready(function(e) {
            var res_code = "${param.res_code}";
			if(res_code != '') {
				if(res_code == "101") {
					appUtil.alert("交易已关闭！");				
				} else if(res_code == "102") {
					appUtil.alert("交易成功！");
				}				
				setTimeout("window.location.href='${ctx}/site/my_order.jsp';",3000);
			}
        });
	 -->
	 </script>
</html>